package org.zoomdev.zoom.hash;

import java.util.Collections;
import java.util.Map;

public class Statistics {
    public final Map<Integer, Long> nodeDistribution;
    final long totalCount;
    final int minUserId;
    final int maxUserId;
    final int minDataPerUser;
    final int maxDataPerUser;

    Statistics(long totalCount, int minUserId, int maxUserId,
               int minDataPerUser, int maxDataPerUser,
               Map<Integer, Long> nodeDistribution) {
        this.totalCount = totalCount;
        this.minUserId = minUserId;
        this.maxUserId = maxUserId;
        this.minDataPerUser = minDataPerUser;
        this.maxDataPerUser = maxDataPerUser;
        this.nodeDistribution = Collections.unmodifiableMap(nodeDistribution);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format(
                "Statistics[total=%,d, users=%d-%d, min/avg/max per user=%,d / %,.1f / %,d]\n",
                totalCount, minUserId, maxUserId, minDataPerUser,
                totalCount / (double) (maxUserId - minUserId + 1), maxDataPerUser));

        sb.append("Node distribution (top 10 by volume):\n");
        nodeDistribution.entrySet().stream()
                .sorted(Map.Entry.<Integer, Long>comparingByValue().reversed())
                //.limit(10)
                .forEach(e -> sb.append(String.format("  Node %4d: %,d (%.2f%%)\n",
                        e.getKey(), e.getValue(), e.getValue() * 100.0 / totalCount)));

        return sb.toString();
    }
}
